﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MedicViet.Models;

namespace MedicViet.Controllers
{ 
    public class RecordController : Controller
    {
        private MedicVietEntities db = new MedicVietEntities();

        //
        // GET: /Record/

        public ViewResult Index()
        {
            var records = db.Records.Include("mvUser");
            ViewBag.indicatorValue = db.RecordIndicators.Include("Record").ToList<RecordIndicator>();
            return View(records.ToList());
        }

        //
        // GET: /Record/Details/5

        public ViewResult Details(int id)
        {
            Record record = db.Records.Single(r => r.id == id);
            return View(record);
        }

        //
        // GET: /Record/Create

        public ActionResult Create()
        {
            ViewBag.userId = new SelectList(db.mvUsers, "id", "username");
            return View();
        } 

        //
        // POST: /Record/Create

        [HttpPost]
        public ActionResult Create(Record record)
        {
            if (ModelState.IsValid)
            {
                db.Records.AddObject(record);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.userId = new SelectList(db.mvUsers, "id", "username", record.userId);
            return View(record);
        }
        
        //
        // GET: /Record/Edit/5
 
        public ActionResult Edit(int id)
        {
            Record record = db.Records.Single(r => r.id == id);
            ViewBag.userId = new SelectList(db.mvUsers, "id", "username", record.userId);
            return View(record);
        }

        //
        // POST: /Record/Edit/5

        [HttpPost]
        public ActionResult Edit(Record record)
        {
            if (ModelState.IsValid)
            {
                db.Records.Attach(record);
                db.ObjectStateManager.ChangeObjectState(record, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.userId = new SelectList(db.mvUsers, "id", "username", record.userId);
            return View(record);
        }

        //
        // GET: /Record/Delete/5
 
        public ActionResult Delete(int id)
        {
            Record record = db.Records.Single(r => r.id == id);
            return View(record);
        }

        //
        // POST: /Record/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Record record = db.Records.Single(r => r.id == id);
            db.Records.DeleteObject(record);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}